package com.whfc.ms.api.emp.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.emp.dto.*;
import com.whfc.emp.param.AppEmpAddParam;
import com.whfc.emp.param.AppEnpApplyCheckParam;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.emp.service.MsAppEmpApplyService;
import com.whfc.ms.api.emp.service.MsAppEmpService;
import com.whfc.ms.api.emp.service.MsAppEmpWorkService;
import com.whfc.ms.api.emp.service.MsAppGroupService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

/**
 * @Description 人员审批
 * @Author hw
 * @Date 2021-08-06 9:16
 * @Version 1.0
 */
@RestController
@RequestMapping("/ms/api/emp/apply")
public class AppEmpApplyController extends BaseController {

    @Autowired
    private MsAppEmpApplyService msAppEmpApplyService;

    @Autowired
    private MsAppEmpService msAppEmpService;

    @Autowired
    private MsAppEmpWorkService msAppEmpWorkService;

    @Autowired
    private MsAppGroupService msAppGroupService;

    @GetMapping("list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam("deptId") Integer deptId,
                       @RequestParam(value = "state", required = false) Integer checkResult,
                       @RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                       @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
                       @RequestParam(value = "pageSize") Integer pageSize,
                       @RequestParam(value = "pageNum") Integer pageNum) {
        logger.info("人员审批列表，loginer:{},depId:{},checkResult:{},startTime:{},endTime:{},pageSize:{},pageNum:{}",
                user.getNickname(), deptId, checkResult, startTime, endTime, pageSize, pageNum);
        PageData<AppEmpApplyDTO> list = msAppEmpApplyService.list(deptId, checkResult, startTime, endTime, pageNum, pageSize);
        return ResultUtil.success(list);
    }

    @GetMapping("num")
    public Result num(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam("deptId") Integer deptId,
                      @RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                      @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) {
        logger.info("人员审批列表，loginer:{},depId:{},startTime:{},endTime:{}",
                user.getNickname(), deptId, startTime, endTime);
        AppEmpApplyNumDTO data = msAppEmpApplyService.num(deptId, startTime, endTime);
        return ResultUtil.success(data);
    }

    @GetMapping("detail")
    public Result detail(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam("empId") Integer empId) {
        logger.info("人员审批详情，loginer:{},empId:{}", user.getNickname(), empId);
        AppEmpApplyDTO data = msAppEmpApplyService.detail(empId);
        return ResultUtil.success(data);
    }

    @RequiresPermissions("emp/approve/deal")
    @PostMapping("check")
    public Result check(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody @Validated AppEnpApplyCheckParam param) {
        logger.info("人员审批，loginer:{},param:{}", user.getNickname(), param);
        param.setCheckName(user.getNickname());
        msAppEmpApplyService.check(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("emp/approve/delete")
    @PostMapping("del/{empId}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("empId") Integer empId) {
        logger.info("人员删除，loginer:{},empId:{}", user.getNickname(), empId);
        msAppEmpApplyService.del(empId);
        return ResultUtil.success();
    }

    @PostMapping("anon/add")
    public Result add(@RequestBody @Validated AppEmpAddParam param) {
        logger.info("人员申请，param:{}", param);
        msAppEmpApplyService.add(param);
        return ResultUtil.success();
    }

    @GetMapping("anon/idCard/identify")
    public Result idCardIdentify(@RequestParam("idCardImgUrl") String idCardImgUrl, HttpSession session) {
        logger.info("人员身份证识别，idCardImgUrl:{}", idCardImgUrl);
        AppEmpIdentifyDTO data = msAppEmpApplyService.idCardIdentify(idCardImgUrl, session.getId());
        return ResultUtil.success(data);
    }

    /**
     * 人员管理-上传图片
     *
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("anon/uploadImg")
    public Result uploadImg(@RequestParam(value = "file", required = false) MultipartFile file,
                            @RequestParam(value = "base64Str", required = false) String base64Str,
                            @RequestParam(value = "faceFlag", required = false) Boolean faceFlag) throws IOException {
        logger.info("人员管理-上传图片");
        String path = msAppEmpService.uploadImg(base64Str, file, faceFlag);
        return ResultUtil.success(path);
    }


    @GetMapping(value = "anon/workType/list")
    public Result workTypeList(@RequestParam("deptId") Integer deptId) {
        logger.info("工种列表(不分页),deptId:{}", deptId);
        ListData<AppWorkTypeDTO> listData = msAppEmpWorkService.workTypeList(deptId);
        return ResultUtil.success(listData);
    }

    @GetMapping("anon/groupListByProjectId")
    public Result groupListByProjectId(@RequestParam("projectId") Integer projectId) {
        logger.info("根据项目查找班组列表,projectId:{}", projectId);
        ListData<AppGroupDTO> listData = msAppGroupService.groupListByProjectId(projectId);
        return ResultUtil.success(listData);
    }
}
